package com.websuites.esb.scheduler;

import java.util.Iterator;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.websuites.core.IEntry;
import com.websuites.core.context.SpringContextHelper;
import com.websuites.core.request.XMLBex;
import com.websuites.core.response.IResult;
import com.websuites.utils.LogUtil;

@SuppressWarnings("unchecked")
public class WsJob implements Job  
{
	public void execute(JobExecutionContext context) throws JobExecutionException
	{
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		Bex bex = (Bex)jobDataMap.get("jobdata");

		XMLBex xmlBex = new XMLBex();
		xmlBex.putBex(bex.getBex());
		
		LogUtil.info("do job " + bex.getBex());
		IEntry entry = (IEntry)SpringContextHelper.getBean("core");
		IResult rs = entry.doBex(null, null, xmlBex);
		String sErrorCode = rs.getErrorCode();
		
		Iterator itListener = bex.getListenerIterator();
		while (itListener.hasNext())
		{
			Listener listener = (Listener)itListener.next();
			String sListenerCode = listener.getCode();
			String sBex = listener.getBex();
			if (!"".equals(sBex) && sErrorCode.equals(sListenerCode))
			{
				XMLBex xmlBex0 = new XMLBex();
				xmlBex0.putBex(sBex);
				entry.doBex(null, null, xmlBex0);
			}
		}
    }
}
